
public class RoundRobin extends SchedulingAlgorithm {
	
	int index;
	int quantum;
	int quantumCount;
	boolean wait;
	boolean emptyQueue;
	
	Process currProc;
		
	public RoundRobin(int q) {
		wait = false;
		index = 0;
		quantum = q;
		quantumCount = 0;
	}
	
	public void RunAReadyProcessForTick() {
		
		index = 0;
		quantumCount++;
					
		while(index < getSizeOfReadyQueue() ) {
			
			//Used to verify that process ran previously is the same as before otherwise increase index 
			if(currProc.processId != readyQueueRef.get(index).processId ) {
				wait = false;
				quantumCount = 0;
			}
			
			//Break loop if list is empty
			if(readyQueueRef.get(index) == null) {
				wait = true;
				break;
			}
			
			if (quantumCount < quantum) {
				
				//Check whether process is in I/O mode and if not, if it has not finished; Breaks loop to run OS Simulator
				if( !readyQueueRef.get(index).IsFinished() && !readyQueueRef.get(index).NeedsIO() && !readyQueueRef.get(index).InIO() && readyQueueRef.get(index).DoneWithIO() ) {
			
					wait = false;
					currProc = readyQueueRef.get(index);
					readyQueueRef.get(index).RunForOneTick();
					readyQueueRef.get(index).NextOperation();
					break;
				}
				else {
					wait = false;
					index++;
					quantumCount = 0;
				}
			}
			else {
				wait = false;
				index++;
				quantumCount = 0;
			}
		
			emptyQueue = true;
			break;
		}
					
	}

	boolean idle() {
		return wait;
	}
	
	//Test whether ready queue is empty because either processes are all in I/O or Finished
	boolean waitoDone() {
		return emptyQueue;
	}
	
}
